import request from '../request';

/**
 * 轮播图配置项类型
 */
export interface CarouselConfig {
  autoplay: boolean;
  autoplaySpeed: number;
  dots: boolean;
  effect: 'scrollx' | 'fade';
}

/**
 * 轮播图项类型
 */
export interface CarouselItem {
  id?: number;
  imageUrl: string;
  title: string;
  description: string;
  buttonText: string;
  buttonLink: string;
  sort?: number;
  status?: number;
  createTime?: string;
  updateTime?: string;
  scene?: string;
}

/**
 * 轮播图完整配置
 */
export interface CarouselConfigData {
  config: CarouselConfig;
  items: CarouselItem[];
  scene?: string;
}

/**
 * 图片上传结果类型
 */
export interface CarouselImageUploadResult {
  imageUrl: string;
  originalFilename: string;
}

// API请求响应类型
interface BaseResponse<T = any> {
  code: number;
  data: T;
  message: string;
}

/**
 * 获取轮播图配置
 */
export async function getCarouselConfig() {
  return request<BaseResponse<CarouselConfigData>>({
    url: '/api/carousel/config',
    method: 'GET',
  });
}

/**
 * 保存轮播图配置
 */
export async function saveCarouselConfig(data: CarouselConfigData) {
  return request<BaseResponse<boolean>>({
    url: '/api/carousel/config',
    method: 'POST',
    data,
  });
}

/**
 * 获取轮播图项列表
 */
export async function listCarouselItems() {
  return request<BaseResponse<CarouselItem[]>>({
    url: '/api/carousel/items',
    method: 'GET',
  });
}

/**
 * 添加轮播图项
 */
export async function addCarouselItem(item: CarouselItem) {
  return request<BaseResponse<CarouselItem>>({
    url: '/api/carousel/item',
    method: 'POST',
    data: item,
  });
}

/**
 * 更新轮播图项
 */
export async function updateCarouselItem(item: CarouselItem) {
  return request<BaseResponse<boolean>>({
    url: '/api/carousel/item',
    method: 'PUT',
    data: item,
  });
}

/**
 * 删除轮播图项
 */
export async function deleteCarouselItem(id: number) {
  return request<BaseResponse<boolean>>({
    url: `/api/carousel/item/${id}`,
    method: 'DELETE',
  });
}

/**
 * 更新轮播图项排序
 */
export async function updateCarouselItemSort(items: { id: number; sort: number }[]) {
  return request<BaseResponse<boolean>>({
    url: '/api/carousel/items/sort',
    method: 'PUT',
    data: items,
  });
}

/**
 * 上传轮播图图片
 * @param file 图片文件
 */
export async function uploadCarouselImage(file: File) {
  const formData = new FormData();
  formData.append('file', file);
  
  return request<BaseResponse<CarouselImageUploadResult>>({
    url: '/api/carousel/upload',
    method: 'POST',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
} 